<?php
/**
 * Created by PhpStorm.
 * User: EZ
 * Date: 2017/6/26
 * Time: 12:23
 */
if(!function_exists('aa')){
    function aa(){

        echo 555;
    }
}

//返回json数据判断成功或错误
function json_success($msg,$data=array()){
    header("content-type:application/json;charset=utf-8");
    header("Access-Control-Allow-Origin:*"); /*星号表示所有的域都可以接受，*/
    header("Access-Control-Allow-Methods:GET,POST");
    return json_encode(array('res'=>1,'msg'=>$msg,'data'=>$data),JSON_UNESCAPED_UNICODE);
}
function json_error($msg,$data=array()){
    header("content-type:application/json;charset=utf-8");
    header("Access-Control-Allow-Origin:*"); /*星号表示所有的域都可以接受，*/
    header("Access-Control-Allow-Methods:GET,POST");
    return json_encode(array('res'=>0,'msg'=>$msg,'data'=>$data),JSON_UNESCAPED_UNICODE);
}

function md5_pwd($pwd,$salt){
    return md5(sha1($salt.sha1($salt.sha1($pwd))));
}

function get_salt(){
    $str="abcdefhijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWSYZ";
    $str=str_shuffle($str);
    $length=mt_rand(4,6);
    return substr($str,mt_rand(0,10),$length);
}

function base64Img($base64Img,$path,$name=''){
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/',$base64Img, $result)){
        $type = $result[2]; //jpeg
        $img = base64_decode(str_replace($result[1], '',$base64Img)); //返回文件流
    }else{
        die(json_error('上传失败'));
    }
    $name=$name?$name:uniqid(true);
    $name.=".".$type;

    if(!is_dir($path)){
        mkdir($path) || die(json_error('文件夹创建失败'));
    }
    $path.='/'.$name;
    file_put_contents($path,$img) || die(json_error('上传失败'));
    return $path;
}
